package com.harboursoftware.xstorage.model;

import java.io.InputStream;
import java.util.Map;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.google.common.collect.ListMultimap;
import com.harboursoftware.xstorage.ac.AccessControlPolicy;

/**
 * 表示一个文件对象。
 * 
 * @author Simon Leung
 * @since 0.1
 * @see XBucket
 * @see XService
 */
public class XObject extends XElement {
    // TODO: s3的key不以/开头
    private String key;
    private XBucket bucket;
    private XUser owner;
    private long length;
    /**
     * TODO 是否把etag并如headers
     */
    private String etag;
    private long lastModified;
    private InputStream inputStream;
    private ListMultimap<String, String> headers;
    private AccessControlPolicy acp;

    public XObject() {
    }

    public XObject(String key) {
        this.setKey(key);
    }

    public XObject(String key, InputStream inputStream) {
        this.setKey(key);
        this.inputStream = inputStream;
    }

    public XObject(String key, XBucket bucket) {
        this.key = key;
        this.bucket = bucket;
    }

    public String getKey() {
        return key;
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    /**
     * set key,key must starts with '/'
     * 
     * @param key
     */
    public void setKey(String key) {
        // TODO : s3中 不以 /开头
        // if(!key.startsWith("/")) {
        // throw new IllegalArgumentException("the key must starts with '/'");
        // }
        this.key = key;
    }

    public XBucket getBucket() {
        return bucket;
    }

    public void setBucket(XBucket bucket) {
        this.bucket = bucket;
    }

    public XUser getOwner() {
        return owner;
    }

    public void setOwner(XUser owner) {
        this.owner = owner;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }

    /**
     * TODO:是否要抛出IOException
     * 
     * @return
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public AccessControlPolicy getAcp() {
        return acp;
    }

    public void setAcp(AccessControlPolicy acp) {
        this.acp = acp;
    }

    public ListMultimap<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(ListMultimap<String, String> headers) {
        this.headers = headers;
    }

    public String getEtag() {
        return etag;
    }

    public void setEtag(String etag) {
        this.etag = etag;
    }

}
